package com.course.controller;

import com.course.entity.ResultBody;
import com.course.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/Register")
@CrossOrigin(origins = "*")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    //学生注册模块
    @RequestMapping("/RegisterUser")
    public ResultBody RegisterUser(@RequestBody Map<String,Object> map)
    {//判断有无缺少信息
        if (map.get("usernumber")==null||map.get("username") == null || map.get("userphone") == null || map.get("userpassword") == null||map.get("repassword")==null||map.get("role")==null)
        {
            return new ResultBody(404, "缺少信息", false);
        }
        String username = map.get("username").toString();//用户名
        String userphone = map.get("userphone").toString();//用户电话号码
        String userpassword = map.get("userpassword").toString();//用户密码
        String usernumber = map.get("usernumber").toString();//用户编号
        String repassword = map.get("repassword").toString();//密码确认
        String role = map.get("role").toString();
        //判断重复密码是否正确
        if(!map.get("userpassword").toString().equals(repassword)) {
            return new ResultBody(400,"重复密码错误",false);
        }
        //检验用户名是否重复
        if(registerService.ReUserName(username)==1)
        {
            return new ResultBody(400,"用户名重复",false);
        }
        //检验电话号码是否重复
        if(registerService.ReUserPhone(userphone)==1)
        {
            return new ResultBody(400,"电话号码重复",false);
        }
        //数据满足条件向下传递
       return registerService.AddUser(username,userphone,userpassword,usernumber,role);
    }
    @RequestMapping("/registerUser")
    public ResultBody registerUser(@RequestBody Map<String,Object> map)
    {
        if(map.get("username")==null ||map.get("password") ==null|| map.get("confirmPassword") ==null||map.get("phone")==null||map.get("email")==null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String username = map.get("username").toString();
        String password1 = map.get("password").toString();
        String password2 = map.get("confirmPassword").toString();
        String phonenumber = map.get("phone").toString();
        String email = map.get("email").toString();
        if(!map.get("password").equals(map.get("confirmPassword"))) {
            return new ResultBody(400,"重复密码错误",false);
        }
        if(registerService.checkPhoneNumber(phonenumber)==1)
        {
            return new ResultBody(400,"电话重复",false);
        }
        if(registerService.checkEmail(email)==1)
        {
            return new ResultBody(400,"邮箱重复",false);
        }
        if(registerService.checkUserName(username)==1)
        {
            return new ResultBody(400,"用户名称重复",false);
        }
        if(registerService. RegisterUser(username, password1, phonenumber,email)==1)
        {
            return new ResultBody(200,"用户注册成功",true);
        }
        else
            return new ResultBody(500,"用户注册失败",false);
    }
}
